/**
 * Created by zhaocong on 2016/7/21.
 * 销售中心
 *
 * @extends timepicker 时间插件
 * @extends tker-css
 * @extends swiper 轮播图
 * @extends swiper-css
 * @extends ./calendar.js 日历组件
 */
define(['timepicker', 'tker-css', 'swiper', 'swiper-css', './calendar.js'], function (require, exports, module) {
  "use strict";
  var cc = window.cc || {};

  /**
   * 销售中心主页
   */
  cc.salesIndex = function () {
    /**
     * 时间插件
     */
    $('.form_datetime').datetimepicker({
      language: 'zh-CN',
      weekStart: 1,
      todayBtn: 1,
      autoclose: 1,
      todayHighlight: 1,
      startView: 2,
      forceParse: 0,
      showMeridian: 1,
      minView: 2,
      format: "yyyy-mm-dd"
    });

    // 点击显示更多筛选选项
    $('.j_hs').click(function () {
      $('.j_hx').toggle();
      $('.j_sx').toggle();
    });

    // 点击筛选项
    $('.app-sxk label').click(function () {
      $(this).addClass('active')
        .siblings().removeClass('active')
        .siblings('input').val($(this).html());
    });

    // 点击调整排序按钮
    $('.j_orderbtn button').click(function () {
      var _i = $(this).find('i');
      _i.hasClass('fa-long-arrow-down') ? _i.removeClass('fa-long-arrow-down').addClass('fa-long-arrow-up')
        : _i.removeClass('fa-long-arrow-up').addClass('fa-long-arrow-down');
    });
  };


  /**
   * 计算器
   * @param callback
   */
  cc.count = function (callback) {
    $('.j_count').on('click', function (e) {
      var nIn = $(this).find('input'),
        min = nIn.data('min') || 0,
        n = parseInt(nIn.val()) || 0;
      $(e.target).is('.j_add') && nIn.val(n + 1);
      $(e.target).is('.j_sub') && n > min && nIn.val(n - 1);
      callback ? callback(n) : null;
    });
  };

  /**
   * 产品详情页面
   */
  cc.salesDetail = function () {

    // 初始化轮播图
    var swiper = new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      mousewheelControl: true,
      mousewheelReleaseOnEdges: true,
      autoplay: 4000,
      loop: true
    });

    // 日历 引用日历模块
    var data = window.data;
    var coy = {tag: {}};
    for (var key in data.tag) {
      coy.tag[key] = '<span class="j_money">￥' +
        data.tag[key].m + '</span><br><span class="j_yue">余' +
        data.tag[key].y + '</span>';
    }
    $.extend(data, coy);
    $('.caaal').cccal(data);

    // 选择日期 预定
    $('.caaal').on('click', 'td', function () {
      if ($(this).find('.j_yue').length && parseInt($(this).find('.j_yue').html().substring(1)) > 0) {
        $(this).parents('.caaal').find('td').removeClass('bg-e4 j_sel');
        $(this).addClass('bg-e4 j_sel');
      }
    });

    // 算数框组件
    cc.count();
  };

  /**
   * 立即预定 - 提交订单
   */
  cc.salesReser1 = function () {
    // 算数框组件

    var app = angular.module('app', []);
    app.controller('appCtrl', ['$scope', function ($scope) {

      // 服务器数据
      $scope.data = window.data;

      // 提交数据
      $scope.form = {}
      // 监听数量变化
      $scope.$watch('data', function () {
        $scope.form.cr = $scope.data.cr.d * $scope.data.cr.n;
        $scope.form.et = $scope.data.et.d * $scope.data.et.n;
        $scope.form.fj = $scope.data.fj.d * $scope.data.fj.n;
        $scope.form.tf = $scope.form.et + $scope.form.cr;
        $scope.form.nop = $scope.data.cr.n + $scope.data.et.n;

        // 计算保险
        $scope.data.bx.forEach(function (n) {
          if (n.val == $scope.data.bxsel) {
            $scope.form.bx = n.price * $scope.form.nop
            $scope.form.selbx = {
              'n': n.name,
              'p': n.price
            }
          }
        });

        // 优惠
        $scope.form.yh = {
          cr: $scope.data.yh.cr * $scope.form.cr,
          et: $scope.data.yh.et * $scope.form.et
        };
        $scope.form.yh.zg = $scope.form.yh.cr + $scope.form.yh.et;

        // 总价
        $scope.form.total = $scope.form.yh.zg + $scope.form.bx + $scope.form.fj;
      }, true);

      $('.j_count').on('click', function (e) {
        var tp = $(this).data('saletype');
        var min = $(this).find('input').data('min') || 0;
        $scope.$apply(function () {
          $(e.target).is('.j_add') && $scope.data[tp].n++;
          $(e.target).is('.j_sub') && $scope.data[tp].n > min && $scope.data[tp].n--;
        });
      });

    }]);
    angular.bootstrap(document.body, ['app']);
  };


  /**
   * 立即预定 - 完善游客信息
   */
  cc.salesReser2 = function () {
    // 算数框组件
    cc.count();

    var app = angular.module('app', []);
    app.controller('appCtrl', ['$scope', function ($scope) {


    }]);
    angular.bootstrap(document.body, ['app']);
  };


  /**
   * 立即预定 - 支付
   */
  cc.salesReser3 = function () {

    $('#save').click(function () {
      // 付款成功
      var suAlert = layer.open({
        title: false,
        btn: false,
        closeBtn: false,
        content: '<div class="text-center"><h3 class="mb20"><i class="fa fa-check-circle-o fa-fw"></i>支付成功</h3> ' +
        '<button type="button" class="btn btn-default btn-lg J_closeSu">取消</button> ' +
        '<button type="button" class="btn btn-warning btn-lg J_sjmodal">完善收据信息</button>' +
        '</div>'
      });
      $('.J_closeSu').on('click', function () {
        layer.close(suAlert);
      });
      $('.J_sjmodal').on('click', function () {
        layer.close(suAlert);
        $('#sjModal').modal({backdrop: 'static'});
      });

    });

  };

  /**
   * 酒店首页
   */
  cc.jdIndex = function () {
    // 初始化轮播图
    var swiper = new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      autoplay: 4000,
      loop: true
    });

    /**
     * 时间插件
     */
    $('.form_datetime').datetimepicker({
      language: 'zh-CN',
      weekStart: 1,
      todayBtn: 1,
      autoclose: 1,
      todayHighlight: 1,
      startView: 2,
      forceParse: 0,
      showMeridian: 1,
      minView: 2,
      format: "yyyy-mm-dd"
    });

    // 点击喜欢
    $('.J_heart').on('click', function () {
      $(this).hasClass('fa-heart-o') ? $(this).addClass('fa-heart').removeClass('fa-heart-o') :
        $(this).addClass('fa-heart-o').removeClass('fa-heart');
    });

  };

  /**
   * 酒店详情
   */
  cc.jdDetail = function () {
    // 初始化轮播图
    var swiper = new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      autoplay: 4000,
      loop: true
    });

  };

  /**
   * 玩乐产品
   */
  cc.wlIndex = function () {
    // 初始化轮播图
    var swiper = new Swiper('.swiper-container', {
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      autoplay: 4000,
      loop: true
    });

    // 菜单
    $('.J_cmlist li').on('mouseenter', function () {
      $(this).removeClass('disabled').siblings().addClass('disabled');
      $('#' + $(this).data('id')).show().siblings().hide();
      $('.flat-item').height($('.J_cmlist').height() - 30);
    });
    $('.slid-as').on('mouseleave', function () {
      $('.J_cmlist li').removeClass('disabled');
      $('.flat-item').hide();
    })
  };

  module.exports = cc;
});